import requests
from lxml import etree
import re
import csv

# 数据的保存（确定我们的保存的表头是什么）
f = open('猫咪.csv', mode='a', encoding='utf-8', newline='')
csv_write = csv.DictWriter(f, fieldnames=['地区',
                                          '标题',
                                          '浏览次数',
                                          '在售数量',
                                          '年龄',
                                          '品种',
                                          '预防'])
csv_write.writeheader()
# 将列表转换成为我们的字符串的操作就是 .join().strip()
url = 'http://www.maomijiaoyi.com/'
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:101.0) Gecko/20100101 Firefox/101.0'
}
res = requests.get(url=url, headers=headers)
tree = etree.HTML(res.text)
url_next_xpath = '//div[@class="breeds_floor"]//div[@class="content"]/a/@href'
area_xpath = '//*[@id="jinjian_cat"]/div/div[2]/a[1]/div[3]/div[2]/span[2]/text()'
next_url = tree.xpath(url_next_xpath)
area_list = re.findall('<div class="area">\s*.*\s.*\s.\s*(.{2})', res.text)
# .strip() 是字符串的方法 将我们的字符串两端的空格进行去掉
detail_list = zip(next_url, area_list)
# http://www.maomijiaoyi.com/index.php?/chanpinxiangqing_456980.html
for url, area in detail_list:
    base_url = f'http://www.maomijiaoyi.com{url}'
    print(area)
    res2 = requests.get(headers=headers, url=base_url)
    tree2 = etree.HTML(res2.text)
    temp_title = tree2.xpath('//div[@class="detail_text"]/div[@class="title"]/text()')
    title = ' '.join(temp_title).strip()  # 这个是我们的title
    price = ' '.join(tree2.xpath('//div[@class="detail_text"]//span[2]/text()')).strip()
    views = ' '.join(tree2.xpath('//div[@class="detail_text"]//span[4]/text()')).strip()
    on_sale = ' '.join(
        tree2.xpath('//div[@class="detail_text"]/div[@class="info2"]/div[1]/div[@class="red"]/text()')).strip()
    age = ' '.join(
        tree2.xpath('//div[@class="detail_text"]/div[@class="info2"]/div[2]/div[@class="red"]/text()')).strip()
    name = ' '.join(
        tree2.xpath('//div[@class="detail_text"]/div[@class="info2"]/div[3]/div[@class="red"]/text()')).strip()
    injection = ' '.join(
        tree2.xpath('//div[@class="detail_text"]/div[@class="info2"]/div[4]/div[@class="red"]/text()')).strip()
    dit = {
        '地区': area,
        '标题': title,
        '浏览次数': views,
        '在售数量': on_sale,
        '年龄': age,
        '品种': name,
        '预防': injection
    }
    csv_write.writerow(dit)
    print('一行成功')
